package 自己写的

import (
	"fmt"
	"io"
	"net/http"
	"os"
	"strconv"
	"time"
)

func main() {
	fmt.Println("开始")
	start := time.Now()
	beke := make(chan bool)
	url_video := "https://resources.cnmyfang.com/2023_02_10_04_17_32_5779fdc5-e63c-49b8-8779-c99a1650a584.mp4"
	for i := 1; i < 6; i++ {
		go DownVideo(url_video, strconv.Itoa(i)+".mp4", beke)
	}
	<-beke
	fmt.Println(time.Now().Sub(start))
	fmt.Println("结束")
}

func DownVideo(url, filepath string, beke chan bool) error {
	//发起请求
	resp, err := http.Get(url)
	if err != nil {
		fmt.Println("视频链接失效")
		return err
	}

	//访问视频的大小
	size, err := strconv.Atoi(resp.Header.Get("Content-Length"))
	if err != nil {
		fmt.Println("字符串转整数失败：", err)
		return err
	}
	fmt.Printf("视频文件的大小为：%d MB\n", size/(1024*1024))

	// Create the file
	file, err := os.Create(filepath)
	if err != nil {
		return err
	}
	defer file.Close()

	// Write the body to file
	_, err = io.Copy(file, resp.Body)
	fmt.Println("视频保存成功")

	if beke != nil {
		beke <- true
	}
	return err
}
